package org.weed.remote.service;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

public class KeyBoardService {
	public static final int KEY_ENTER = KeyEvent.VK_ENTER;
	public static final int KEY_UP = KeyEvent.VK_UP;
	public static final int KEY_DOWN = KeyEvent.VK_DOWN;
	public static final int KEY_LEFT = KeyEvent.VK_LEFT;
	public static final int KEY_RIGHT = KeyEvent.VK_RIGHT;
	private Robot robot;
	private Map<Character, Integer> keycodes;
	
	public KeyBoardService(Robot robot) {
		this.robot = robot;		
		initKeycodes();
	}
	
	public void initKeycodes() {
		keycodes = new HashMap<Character, Integer>();
		keycodes.put('0', KeyEvent.VK_0);
		keycodes.put('1', KeyEvent.VK_1);
		keycodes.put('2', KeyEvent.VK_2);
		keycodes.put('3', KeyEvent.VK_3);
		keycodes.put('4', KeyEvent.VK_4);
		keycodes.put('5', KeyEvent.VK_5);
		keycodes.put('6', KeyEvent.VK_6);
		keycodes.put('7', KeyEvent.VK_7);
		keycodes.put('8', KeyEvent.VK_8);
		keycodes.put('9', KeyEvent.VK_9);
		keycodes.put('<', KeyEvent.VK_LEFT);
		keycodes.put('>', KeyEvent.VK_RIGHT);
		keycodes.put('+', KeyEvent.VK_UP);
		keycodes.put('-', KeyEvent.VK_DOWN);	
		keycodes.put('/', KeyEvent.VK_ENTER);
	}
	
	public void keyClick(String keySequence) {
		for (int i = 0; i < keySequence.length(); i++) {
			System.out.println("Click " + keySequence.charAt(i));
			keyClick(keycodes.get(keySequence.charAt(i)));
		}
	}
	

	
	
	
	public void keyClick(int keycode) {
		robot.keyPress(keycode);
		robot.keyRelease(keycode);
	}
	
	public static void main(String[] args) throws AWTException {
		Robot robot = new Robot();
		robot.keyPress(KeyEvent.VK_4);
		robot.keyRelease(KeyEvent.VK_4);
	}
}
